$(document).ready(function(){
	$('#menu-left a').each(function(){
		var it_rel = $(this).attr('rel');
		if (it_rel == cur_router) {
			$('#menu-left li.active').removeClass('active');
			$(this).parent('li').addClass('active');
		};
	});

	$('#myTab a:first').tab('show');

	//Get state
	$('#sl_country').change(function() {
		var id = $(this).val();
		$.ajax({
			type: "POST",
			dataType: "html",
			data: "region_id=" + id,
			url: baseUrlJs + "add_person/get_sub_region",
			success: function(data) {
				$('#sub_region').html(data);
			}
		});
	});

	$(document).mouseup(function (e)
	{
		var container = $("#photo_preview");
		if (container.has(e.target).length === 0)
		{
			closePhotoComment();
		}
	});

});

function show(element){
	$('#' + element).show();
}
function hide(element){
	$('#' + element).hide();
}


function send_email_confirm(){
	var email_get_start = $('#email_get_start').val();
	if(email_get_start == '') {
		$('#email_get_start').focus();	
	} else {
		show('show_load_1');
		$.ajax({
			type: "POST",
			dataType:"json",
			data: "email_address=" + email_get_start,
			url: baseUrlJs + "register",
			success: function(data){
				if(data.status)	{
					alert(data.message);
					hide('show_load_1');
					$('#email_get_start').val('');
				} else {
					alert(data.message);
					hide('show_load_1');
					$('#email_get_start').val('');
				}
			}
		});
	}
}

function action_step1(){
	var fullname = $('#fullname');
	var password = $('#password');
	if(fullname.val() == '') {
		fullname.focus();
	} else if (password.val() == ''){
		password.focus();
	} else {
		show('show_load_1');
		$.ajax({
			type: "POST",
			dataType:"json",
			data: "fullname=" + fullname.val() + "&password=" + password.val() + "&code_confirm=" + $('#hide_code').val(),
			url: baseUrlJs + "register/complete",
			success: function(data){
				window.location.href = baseUrlJs + 'home';
			}
		});	
	}
}

function update_info(){
	var fullname = $('#fullname');
	var password = $('#password');
	if(fullname.val() == '') {
		fullname.focus();
	} else if (password.val() == ''){
		password.focus();
	} else {
		show('show_load_1');
		$.ajax({
			type: "POST",
			dataType:"json",
			data: "fullname=" + fullname.val() + "&password=" + password.val() + "&memId=" + $('#hide_memId').val(),
			url: baseUrlJs + "register/update_info",
			success: function(data){
				window.location.href = baseUrlJs + 'home';
			}
		});	
	}
}

function login(){
	var email_address = $('#log_email');
	var pass = $('#log_pass');
	if(email_address.val() == '') {
		email_address.focus();
	} else if (pass.val() == ''){
		pass.focus();
	} else {
		$('#btn_login').attr("disabled", "disabled");
		$.ajax({
			type: "POST",
			dataType:"json",
			data: "email=" + email_address.val() + "&password=" + pass.val(),
			url: baseUrlJs + "landing/login",
			success: function(data){
				if(data.status) {
					window.location.href = baseUrlJs + 'home';	
				} else {
					$("p.error").text('');
					$("p.error").text(data.message);
					$('#btn_login').removeAttr("disabled");
				}
				
			}
		});
	}
}


/*Laod list photo person*/
function loadListPhotoPerson(personId) {
	$.ajax({
		type: "POST",
		dataType: "html",
		data: "personId=" + personId,
		url: baseUrlJs + "person/loadListPhotoPerson",
		success: function(data){
				
			$('#list-photo-person').html(data);

		}
	});
}

function post_comment(memId, memAvatar, personId) {
	if($('#txt_comment').val() == '') {
		$('#txt_comment').focus();
	} else {
		$('#btn_post_comment').text('Posting...').attr('disabled', 'disabled');
		$.ajax({
			type: "POST",
			dataType: "json",
			data: "memId=" + memId + "&memAvatar=" + memAvatar + "&personId=" + personId + "&comment=" + $('#txt_comment').val(),
			url: baseUrlJs + "person/post_comment",
			success: function(data) {
				if(data.status) {
					$('#btn_post_comment').text('Post').removeAttr('disabled');	
					$('#txt_comment').val('');
					loadListComment(personId);
					load_total_comment(personId);
				} else {
					$('#btn_post_comment').text('Post').removeAttr('disabled');
					$('#txt_comment').val('');
					alert(data.message);	
				}
			}
		});	
	}
}

function loadListComment(personId) {
	show('loading_list');
	$.ajax({
			type: "POST",
			dataType: "html",
			data: "personId=" + personId,
			url: baseUrlJs + "person/get_comment",
			success: function(data) {
				if(data) {
					$('#show-list-comment').html(data);
					hide('loading_list');
					$('#load_more_comment').show();
				} else {
					hide('loading_list');
					$('#load_more_comment').hide();
				}
				
			}
		});	
}
$start = 4;
function load_more_comment(personId) {
	
	$('#load_more_comment a').text('Loading...');
	var $top = $('#show-list-comment div.row:last').offset().top;
	$.ajax({
		type: "POST",
		dataType: "html",
		data: "personId=" + personId + "&start=" + $start,
		url: baseUrlJs + "person/get_more_comment",
		success: function(data) {
		if(data) {
			$('#show-list-comment').append(data);
				$start +=4;
				hide('loading_list');
				$('html, body').animate({scrollTop: $top});	
				$('#load_more_comment a').text('Show more');
			} else {
				$('#load_more_comment a').text('Show more');
				$('#load_more_comment').remove();
				//hide('loading_list');	
			}
				
		}	
	});
}

function load_total_comment(personId) {
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "personId=" + personId,
		url: baseUrlJs + "person/load_total_comment",
		success: function(data) {
			if(data) {
				$('#total-comment').html(data.total);			
			}	
		}
	});	
}

function add_subscribed(memId, personId) {
	$('#add_subscribed').text('Processing...');
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "personId=" + personId + "&memId=" + memId,
		url: baseUrlJs + "person/add_subscribed",
		success: function(data) {
		if(data.status) {
			$('#add_subscribed').html('<i class="icon-chevron-down"></i> Subsribed');
			check_status_click_subscribed(memId, personId);
			load_total_subscribed(personId);
			} else {
				$('#add_subscribed').html('<i class="icon-chevron-down"></i> Subsribed');
				alert(data.message);
			}
				
		}	
	});
}

function load_total_subscribed(personId){
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "personId=" + personId,
		url: baseUrlJs + "person/load_total_subscribed",
		success: function(data) {
			if(data) {
				$('#total-count').html(data.total);			
			}	
		}
	});	
}

function check_status_click_subscribed(memId, personId) {
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "personId=" + personId + "&memId=" + memId,
		url: baseUrlJs + "person/check_status_subscribed",
		success: function(data) {
		if(data.status) {
			$('#add_subscribed').attr('disabled', 'disabled');
			} else {
				$('#add_subscribed').removeAttr('disabled');
			}
				
		}	
	});	
}

function add_video(memId, personId) {
	if($('#url_video').val() == '') {
		$('#url_video').focus();
	} else {
		$('#btn-add_video').text('Processing...');
		$.ajax({
			type: "POST",
			dataType: "json",
			data: "personId=" + personId + "&memId=" + memId + "&url_video=" + $('#url_video').val(),
			url: baseUrlJs + "person/add_video",
			success: function(data) {
			if(data.status) {
				//$('#add_subscribed').attr('disabled', 'disabled');
				$('#btn-add_video').text('Post');
				$('#url_video').val('');
				load_videos(personId);
				} else {
					$('#btn-add_video').text('Post');
				}
					
			}	
		});	
	}
}

function load_videos(personId) {
	$.ajax({
		type: "POST",
		dataType: "html",
		data: "personId=" + personId,
		url: baseUrlJs + "person/load_video",
		success: function(data) {
		if(data) {
			$('#list-video-person').html(data);
			} else {
				$('#list-video-person').html('<p>No video</p>');
			}
				
		}	
	});
}

$start_video = 3;
function load_more_video(personId) {
	
	$('#load_more_video_btn').text('Loading...');
	var $top = $('#list-video-person div.col-lg-4:last').offset().top;
	$.ajax({
		type: "POST",
		dataType: "html",
		data: "personId=" + personId + "&start=" + $start_video,
		url: baseUrlJs + "person/get_more_video",
		success: function(data) {
		if(data) {
			$('#list-video-person').append(data);
				$start_video +=3;
				$('html, body').animate({scrollTop: $top});	
				$('#load_more_video_btn').text('Load more video');
			} else {
				$('#load_more_video_btn').text('Load more video');
				$('#load_more_video_btn').remove();
				//hide('loading_list');	
			}
				
		}	
	});
}


function like_video(personId, memId, videoId){
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "personId=" + personId + "&memId=" + memId + "&videoId=" + videoId,
		url: baseUrlJs + "person/like_video",
		success: function(data) {
		if(data.status) { 
			//check_status_like_video(memId, personId);
			load_total_like_video(videoId);
			} else { 
				alert(data.message);
			}
				
		}	
	});
}

function dislike_video(personId, memId, videoId){
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "personId=" + personId + "&memId=" + memId + "&videoId=" + videoId,
		url: baseUrlJs + "person/dislike_video",
		success: function(data) {
		if(data.status) { 
			//check_status_dislike_video(memId, personId);
			load_total_dislike_video(videoId);
			} else { 
				alert(data.message);
			}
		}	
	});
}


function load_total_like_video(videoId){
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "videoId=" + videoId,
		url: baseUrlJs + "person/load_total_like_video",
		success: function(data) {
			if(data) {
				$('#like_video_' + videoId).html(data.total);			
			}	
		}
	});	
}

function load_total_dislike_video(videoId){
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "videoId=" + videoId,
		url: baseUrlJs + "person/load_total_dislike_video",
		success: function(data) {
			if(data) {
				$('#dislike_video_' + videoId).html(data.total);			
			}	
		}
	});	
} 

function getPhotoComment(photoId,personId) {

	$.ajax({
		type: "POST",
		dataType: "json",
		data: "photoId=" + photoId + "&personId=" + personId,
		url: baseUrlJs + "person/getPhotoComment",
		success: function(data) {
			if(data.status) {
				$('#photo_preview .column_left').html('<div class="main_pop_photo">' + data.dataImage + '</div>');
				$('#photo_preview .column_right').html(data.dataComment); 
				$('#photo_preview').show();
				getCommentPhoto(photoId);
			} else {
				alert(data.message);
			}
		}	
	});
};


function post_commentPhoto(memId, memAvatar, photoId) {
	if($('#txt_commentPhoto').val() == '') {
		$('#txt_commentPhoto').focus();
	} else {
		$('#btn_post_commentPhoto').text('Posting...').attr('disabled', 'disabled');
		$.ajax({
			type: "POST",
			dataType: "json",
			data: "memId=" + memId + "&memAvatar=" + memAvatar + "&photoId=" + photoId + "&commentPhoto=" + $('#txt_commentPhoto').val(),
			url: baseUrlJs + "person/post_commentPhoto",
			success: function(data) {
				if(data.status) {
					$('#btn_post_commentPhoto').text('Add Comment').removeAttr('disabled');	
					$('#txt_commentPhoto').val('');
					getCommentPhoto(photoId);
					//load_total_comment(personId);
				} else {
					$('#btn_post_commentPhoto').text('Add Comment').removeAttr('disabled');
					$('#txt_commentPhoto').val('');
					alert(data.message);	
				}
			}
		});	
	}
}


function getCommentPhoto(photoId) {
	show('loading_list_photo');
	$.ajax({
			type: "POST",
			dataType: "html",
			data: "photoId=" + photoId,
			url: baseUrlJs + "person/getCommentPhoto",
			success: function(data) {
				if(data) {
					$('#show-list-commentPhoto').html(data);
					hide('loading_list_photo');
					$("#show-list-commentPhoto").mCustomScrollbar({
						scrollButtons: {
							enable: true
						}
					}); 
					//$('#load_more_comment').show();
				} else {
					hide('loading_list_photo');
					//$('#load_more_comment').hide();
				}
				
			}
		});	
}

function closePhotoComment() {
    $('#photo_preview').hide();
    $('#photo_preview .column_left').html('empty');
	$('#photo_preview .column_right').html('empty');
};

function remove_photo(id) {
	$.ajax({
		type: "POST",
		dataType: "json",
		data: "id=" + id,
		url: baseUrlJs + "person/remove_photo",
		success: function(data) {
			if(data.status) {
				$('#photo_' + id).remove();
			} else {
				alert(data.message);
			}
		}	
	});
}


//Social
function return_shared_on_facebook(title, desc, url, image) {
	window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]='+ title +'&amp;p[summary]='+ desc +'&amp;p[url]='+url+'&amp;p[images][0]=' + image,'sharer','toolbar=0,status=0,width=548,height=325');
}